<template>
  <a-modal
    title="编辑采购订单"
    width="80%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="订单编码"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input readOnly placeholder="请输入采购订单编码" v-decorator="['code', {rules: [{required: false, message: '请输入采购订单编码！'}]}]" />
        </a-form-item>
        <a-form-item
          label="到货时间"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-date-picker
            style="width: 100%"
            :show-time="{ format: 'HH:mm:ss' }"
            format="YYYY-MM-DD HH:mm:ss"
            placeholder=""
            v-decorator="['arrivalTime',{rules: [{ required: true, message: '请选择到货时间！' }]}]"
            @change="arrivalTimeOnChange"/>
        </a-form-item>
        <a-form-item
          label="订单来源"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-select
            allowClear
            placeholder="请输入订单来源"
            v-decorator="['orderSource',{rules: [{ required: true, message: '请输入订单来源！'}]}]">
            <a-select-option
              v-for="(suppItem,suppIndex) in suppDataList"
              :key="suppIndex"
              :value="suppItem.id">{{ suppItem.suppDataName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <puor-order-table-form-item
          :dataSource="puorDetailList"
          :proList="list.proList"
          :purchaseNum="form.getFieldValue('purchaseNum') ? form.getFieldValue('purchaseNum') : null"
          :unitPrice="form.getFieldValue('unitPrice') ? form.getFieldValue('unitPrice') : null"
          :totalPrice="form.getFieldValue('totalPrice') ? form.getFieldValue('totalPrice') : null"
          @add="
            ({ tableData }) => {
              puorDetailList = tableData
            }
          "
          @del="
            ({ tableData }) => {
              puorDetailList = tableData
            }
          "
        />
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-textarea
            :rows="3"
            placeholder="请输入备注信息"
            v-decorator="['remarks']"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { puorOrderEdit } from '@/api/modular/main/puororder/puorOrderManage'
  import { suppDataList } from '@/api/modular/main/suppdata/suppDataManage'
  import puorOrderTableFormItem from '@/views/main/puororder/puorOrderTableFormltem'
  import { proList } from '@/api/modular/main/pro/proManage'
  import moment from 'moment'
  export default {
    components: {
      puorOrderTableFormItem
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 3 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 20 }
        },
        list: {
          // 产品
          proList: []
        },
        puorDetailList: [],
        arrivalTimeDateString: '',
        visible: false,
        suppDataList: [],
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      moment,
      // 初始化方法
      edit (record) {
        this.visible = true
        this.puorDetailList = record.puorDetailList
        proList().then((res) => {
          this.list.proList = res.data
        })
        suppDataList().then(res => {
          this.suppDataList = res.data
        })
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              code: record.code,
              orderSource: record.orderSource,
              remarks: record.remarks
            }
          )
        }, 100)
        // 时间单独处理
        if (record.arrivalTime) {
          this.form.getFieldDecorator('arrivalTime', { initialValue: moment(record.arrivalTime, 'YYYY-MM-DD HH:mm:ss') })
          this.arrivalTimeDateString = moment(record.arrivalTime).format('YYYY-MM-DD HH:mm:ss')
        }
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            values.arrivalTime = this.arrivalTimeDateString || null
            values.puorDetailList = [...this.puorDetailList]
            // 去除编号空格
            values.code = values.code ? values.code.trim() : values.code
            puorOrderEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.message)//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      arrivalTimeOnChange(date, dateString) {
        this.arrivalTimeDateString = dateString
        this.form.setFieldsValue({
          arrivalTime: moment()
        })
      },
      handleCancel () {
        this.arrivalTimeDateString = ''
        this.form.getFieldDecorator('arrivalTime', { initialValue: null })
        this.form.resetFields()
        this.visible = false
        this.$emit('ok')
      }
    }
  }
</script>
